<?php

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Database\Seeder;
use App\Models\Tag;

class TagsSeed extends Seeder
{
    protected $_import_file_name = 'tags.yml';
    /**
     * Run the database seeds.
     *
     * @return void
     * @throws
     */
    public function run()
    {
        $file = database_path("seeds/{$this->_import_file_name}");
        if (! file_exists($file)) {
            throw new FileNotFoundException('Not found tags.yml');
        }
        $tags = yaml_parse_file($file);

        Tag::unguard();
        foreach($tags as $tag) {
            Tag::firstOrCreate(['name' => $tag]);
        }
        $count = count($tags);
        echo "共创建标签: $count 个";
    }
}
